<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$date = (isset($_POST["date"])) ? $_POST["date"] : "" ;
	$act = (isset($_POST["act"])) ? $_POST["act"] : "" ;

	switch ($act) {
		case 'dayviewroom':
			
			break;
			
		case 'monthviewroom':
			if ($date != '') {
				$exdate = explode("-",$date);
				$jumHari = cal_days_in_month(CAL_GREGORIAN, $exdate[1], $exdate[0]);

				$sql_room = "SELECT a.id, a.nama FROM room a";
				$query_room = mysql_query($sql_room);
				$count_room = mysql_num_rows($query_room);
				if ($count_room > 0) {
					?>
					<thead>
						<tr>
							<th class="al_center" valign="middle" rowspan="2" style="vertical-align:middle">No</th>
							<th class="al_center" valign="middle" rowspan="2" style="vertical-align:middle">Room Name</th>
							<th class="al_center" valign="middle" colspan="<?php echo $jumHari ?>" style="vertical-align:middle">Date</th>
							<th class="al_center" valign="middle" rowspan="2" style="vertical-align:middle">Accopier Amount</th>
						</tr>
						<tr>
							<?php
								for ($m = 1; $m <= $jumHari ; $m++) { 
									?>
									<th class="al_center" valign="middle"><?php echo $m ?></th>
									<?php
								}
							?>
						</tr>
					</thead>
					<tbody>
						<?php
							$i = 1;
							$query_room = mysql_query($sql_room);
							while ($baris_room = mysql_fetch_array($query_room)) {
								?>
								<tr>
									<td class="al_center" valign="middle"><?php echo $i ?></td>
									<td class="al_center" valign="middle"><?php echo $baris_room['nama'] ?></td>
										<?php
											$amount_room = 0;
											for ($j=1; $j <= $jumHari ; $j++) {
												$sql_date_room = "SELECT COUNT(checkout) AS amount, 
												DATE_FORMAT(checkin, '%e') as checkin_date,
												DATE_FORMAT(checkout, '%e') as checkout_date
												FROM front_office WHERE idroom = '".$baris_room['id']."' AND is_paid = 1 
												AND YEAR(checkout) = '".$exdate[0]."' AND MONTH(checkout) = '".$exdate[1]."'
												AND (".$j." BETWEEN DATE_FORMAT(checkin, '%e') AND DATE_FORMAT(checkout, '%e')) ORDER BY checkout";
												$query_date_room = mysql_query($sql_date_room);
												$date_room = mysql_fetch_array($query_date_room);
												if ($date_room['amount'] < 1) {
													?>
													<td class="al_center invalid-row" valign="middle">-</td>
													<?php
												}else{
													if ($j == $date_room['checkin_date']) {
														?>
														<td class="al_center checkin-cat" valign="middle">
															<table class="piranti-in">
																<tr>
																	<td class="room">&nbsp</td>
																	<td class="room">&nbsp</td>
																</tr>
															</table>
														</td>	
														<?php
													}elseif($j == $date_room['checkout_date']){
														$sql_validasi_checkin = "SELECT COUNT(checkin) AS hasil_cek FROM front_office a 
														WHERE a.idroom = '".$baris_room['id']."' AND YEAR(checkin) = '".$exdate[0]."' 
														AND MONTH(checkin) = '".$exdate[1]."' AND DATE_FORMAT(checkin, '%e') = '".$j."'";
														$query_validasi_checkin = mysql_query($sql_validasi_checkin);
														$validasi_checkin = mysql_fetch_array($query_validasi_checkin);
														if ($validasi_checkin['hasil_cek'] < 1) {
															$status_validasi = "";
														}else{
															$status_validasi = "checkin-cat";
														}
														?>
														<td class="al_center checkout-cat <?php echo $status_validasi ?>" valign="middle">
															<table class="piranti-in">
																<tr>
																	<td class="room">&nbsp</td>
																	<td class="room">&nbsp</td>
																</tr>
															</table>
														</td>	
														<?php
													}else{
														?>
														<td class="al_center valid-row" valign="middle">
															<table class="piranti-in">
																<tr>
																	<td class="room">&nbsp</td>
																</tr>
															</table>
														</td>
														<?php
													}

													if ($date_room[checkout_date] == $j) {
														$amount_room = $amount_room+1;
													}
												}
											}
										?>
										<td class="al_center" valign="middle"><?php echo $amount_room ?></td>
									</td>
								</tr>
								<?php
								$i++;
								$total = $total+$amount_room;
							}
						?>
						<tr>
							<td colspan="<?php echo $jumHari+2 ?>" class="al_right" valign="middle">Accopier Amount</td>
							<td class="al_center" valign="middle"><?php echo $total ?></td>
						</tr>
					</tbody>
					<?php
				}
			}
			break;

		case 'yearviewroom':
			if ($date != '') {
				$sql_room = "SELECT a.id, a.nama FROM room a";
				$query_room = mysql_query($sql_room);
				$count_room = mysql_num_rows($query_room);
				if ($count_room > 0) {
					?>
					<thead>
						<tr>
							<th class="al_center" valign="middle">No</th>
							<th class="al_center" valign="middle">Room Name</th>
							<?php
								$month_name = array("Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
								for ($m = 1; $m <= 12 ; $m++) { 
									?>
									<th class="al_center" valign="middle"><?php echo $month_name[$m-1] ?></th>
									<?php
								}
							?>
							<th class="al_center" valign="middle">Amount</th>
						</tr>
					</thead>
					<tbody>
						<?php
							$i = 1;
							$query_room = mysql_query($sql_room);
							while ($baris_room = mysql_fetch_array($query_room)) {
								?>
								<tr>
									<td class="al_center" valign="middle"><?php echo $i ?></td>
									<td class="al_center" valign="middle"><?php echo $baris_room['nama'] ?></td>
										<?php
											$amount_room = 0;
											for ($j=1; $j <= 12 ; $j++) { 
												$sql_date_room = "SELECT MONTH(checkout) AS month, COUNT(checkout) AS amount 
												FROM front_office WHERE idroom = '".$baris_room['id']."' AND YEAR(checkout) = '".$date."' AND MONTH(checkout) = '".$j."'";
												$query_date_room = mysql_query($sql_date_room);
												$date_room = mysql_fetch_array($query_date_room);
												if ($date_room['amount'] < 1) {
													?>
													<td class="al_center" valign="middle">-</td>
													<?php
												}else{
													?>
													<td class="al_center" valign="middle">
														<?php echo $date_room['amount'] ?>
													</td>
													<?php
													$amount_room = $amount_room+$date_room['amount'];
												}
											}
										?>
										<td class="al_center" valign="middle"><?php echo $amount_room ?></td>
									</td>
								</tr>
								<?php
								$i++;
								$total = $total+$amount_room;
							}
						?>
						<tr>
							<td colspan="2" class="al_center" valign="middle">Amount</td>
							<?php
								for ($j=1; $j <= 12 ; $j++) { 
									$sql_date_amount = "SELECT MONTH(checkout) AS month, COUNT(checkout) AS amount 
									FROM front_office WHERE YEAR(checkout) = '".$date."' AND MONTH(checkout) = '".$j."'";
									$query_date_amount = mysql_query($sql_date_amount);
									$date_amount = mysql_fetch_array($query_date_amount);
									?>
										<td class="al_center" valign="middle">
											<?php echo $date_amount['amount'] ?>
										</td>
									<?php
								}
							?>
							<td class="al_center" valign="middle"><?php echo $total ?></td>
						</tr>
					</tbody>
					<?php
				}
			}
			break;
		
		default:
			# code...
			break;
	}
?>